<div id="form"></div>
<script>
var leftcolw=360;
var rightcolw;
var twocol=true;

function window_resize()
{
	var wx=win_x();
	if (wx>1150) {
		leftcolw=360;
		rightcolw=wx-360-200-150;
		twocol=true;
	} else {
		if (wx<700) wx=700;
		leftcolw=wx-200-150;
		rightcolw=wx-100;
		twocol=false;
	}

	if (twocol) {
		$(".af-peditcol1").css("display","inline-block");
		$(".af-peditcol2").css("display","inline-block");
	} else {
		$(".af-peditcol1").css("display","block");
		$(".af-peditcol2").css("display","block");
	}
	$(".af-peditcol1").width(leftcolw+200+60);
	$(".af-peditcol2").width(rightcolw);
	
	$(".af-peditcol1 .leftcolw1 .af-edit").width(leftcolw);
	$(".af-peditcol1 .leftcolw2 .af-edit").width(leftcolw-10);
	
}


var frm=$("#form").ajajform({
	header: "{{!title}}",
	layouttemplate: '<div class="af-peditcol1"></div><div class="af-peditcol2"></div><div style="clear: both;"></div>',
	submit: {
		url: '{{action}}',
		value: "{{!!save_and_exit}}",
		ajaj: true,
		click: function() { frm.ajajform("setSubmitParam","mode","save"); return 1; },
		params: {mode: "save",page_id: '{{fields.page_id}}',ajaj: 1}
	},
	footerButtons: [{value: "{{!!save_and_continue}}", click: function() { frm.ajajform("setSubmitParam","mode","save_only");frm.ajajform("submitClicked"); } }]
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "select",
	name: "template_id",
	label: "{{!field_template}}",
	values: {{JSON:templates}},
	validate: function(x) {if (!x || x=="" || x=="0") return "{{!field_template_empty_error}}";},
	change: function() {call_page_editor_rebuild_blocks();},
	width: leftcolw,
	cssclass: "afw-select leftcolw1",
	value: "{{cpage.template_id}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "select",
	name: "parent_id",
	label: "{{!field_parent_page}}",
	values: {{JSON:pages}},
	width: leftcolw,
	change: function() { try_update_alias(); },
	cssclass: "afw-select leftcolw1",
	value: "{{cpage.parent_id}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "textedit",
	name: "alias",
	label: "{{!field_alias}}",
	validate: function(x) {
			if (!x || x.length==0) return "{{!field_alias_empty_error}}";
			if (!x.match(/^\//)) return "{{!field_alias_no_head_slash}}";
			if (!x.match(/\/$/)) return "{{!field_alias_no_tail_slash}}";
			if (x.match(/\/\//)) return "{{!field_alias_mismatch_error}}";
			if (!x.match(/^\/([\w_\-]+\/)+$/)) return "{{!field_alias_mismatch_error}}";
	},
	width: leftcolw-10,
	cssclass: "afw-textedit leftcolw2",
	value: "{{JS:cpage.alias}}"
});
var inputs_name={{JSON:inputs_name}};
for (var i=0;i<inputs_name.length;i++) {
	var n=inputs_name[i];
	frm.ajajform("insertWidget",'.af-peditcol1',null,{
		type: "textedit",
		name: n.name,
		label: "{{!field_name}}"+(n.language==""?"":" ("+n.language+")"),
		validate: function(x) {if (!x || x.length==0) return "{{!field_name_empty_error}}";},
		width: leftcolw-10,
		cssclass: "afw-textedit leftcolw2",
		value: n.value
	});
}
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "textedit",
	name: "prerun",
	label: "{{!field_prerun}}",
	width: leftcolw-10,
	cssclass: "afw-textedit leftcolw2",
	value: "{{JS:cpage.prerun}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "textareaedit",
	name: "prerun_param",
	label: "{{!field_prerun_param}}",
	height: 60,
	width: leftcolw-10,
	cssclass: "afw-textareaedit leftcolw2",
	value: "{{JS:cpage.prerun_param}}"
});
var inputs_ktsd={{JSON:inputs_ktsd}};
for (var i=0;i<inputs_ktsd.length;i++) {
	var n=inputs_ktsd[i];
	frm.ajajform("insertWidget",'.af-peditcol1',null,{
		type: "textedit",
		name: n.name,
		label: n.label+(n.language==""?"":" ("+n.language+")"),
		width: leftcolw-10,
		cssclass: "afw-textedit leftcolw2",
		value: n.value
	});
}
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "checkbox",
	name: "show_in_nav",
	label: "{{!field_show_in_nav}}",
	width: leftcolw-10,
	cssclass: "afw-checkbox leftcolw2",
	value: "{{cpage.show_in_nav}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "checkbox",
	name: "hide_anon",
	label: "{{!field_hide_anon}}",
	width: leftcolw-10,
	cssclass: "afw-checkbox leftcolw2",
	value: "{{cpage.hide_anon}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "checkbox",
	name: "hide_reg",
	label: "{{!field_hide_reg}}",
	width: leftcolw-10,
	cssclass: "afw-checkbox leftcolw2",
	value: "{{cpage.hide_reg}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "select",
	name: "role_id",
	label: "{{!field_role_id}}",
	values: {{JSON:roles}},
	width: leftcolw,
	cssclass: "afw-select leftcolw1",
	value: "{{cpage.role_id}}"
});
frm.ajajform("insertWidget",'.af-peditcol1',null,{
	type: "textedit",
	name: "cssclass",
	label: "{{!field_cssclass}}",
	width: leftcolw-10,
	cssclass: "afw-textedit leftcolw2",
	value: "{{JS:cpage.cssclass}}"
});

var tplaces={{JSON:tplaces}};
{{if cpage_blocks}}var cpage_blocks={{JSON:cpage_blocks}};{{/if}}
{{unless cpage_blocks}}var cpage_blocks=[];/*empty*/{{/unless}}

function call_page_editor_rebuild_blocks()
{
	page_editor_rebuild_blocks(frm,tplaces[frm.ajajform("getWidgetValue","template_id")],cpage_blocks,{{JSON:site.multiple_languages}},{{JSON:gdbe_styles}})
}
call_page_editor_rebuild_blocks();

var parent_ids={{JSON:pages}};
var alias_set="{{JS:cpage.alias}}"==""?0:1;
var namefield;

function try_update_alias()
{
	var val=frm.ajajform("getWidgetValue","alias");
	if (alias_set && val!="") return;
	var parent_id=frm.ajajform("getWidgetValue","parent_id");
	if (!namefield) return;
	var pagename=frm.ajajform("getWidgetValue",namefield);
	var newname="/";
	if (parent_id) {
		for (var i=0;i<parent_ids.length;i++) if (parent_ids[i].id==parent_id) newname=parent_ids[i].name.replace(/^\s+/,"");
	}
	if (!pagename) {
		frm.ajajform("setWidgetValue","alias",newname+"/");
		return;
	}
	$.ajax({
		type: "POST",
		url: "/admin-pages/",
		data: {
			mode:"translit",
			ajaj:1,
			value: pagename
		},
		success: function(r) {
			newname+=r;
			newname+="/";
			frm.ajajform("setWidgetValue","alias",newname);
		}
	});
}
for (var k in frm.ajajform("getWidgets")) {
	if (!namefield && k.match(/^name(_\w+|)$/)) namefield=k;
}
if (namefield) {
	frm.ajajform("setWidgetOption",namefield,"change",try_update_alias);
}
window_resize();

</script>

